package com.heima.admin.aop;


import com.heima.admin.interceptor.AdminTheadLocal;
import com.heima.admin.mapper.ApUserOperateLogMapper;
import com.heima.model.admin.pojos.AdUserOperateLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Aspect
public class ApUserOperateLogAspect {

    @Autowired
    private ApUserOperateLogMapper apUserOperateLogMapper;

    @Around("execution(* com.heima.admin.controller.*.*(..))&&@annotation(com.heima.admin.anno.Log)")
    public Object recodeLog(ProceedingJoinPoint joinPoint) throws Throwable {

        Integer id = Integer.valueOf(AdminTheadLocal.getAdUserid());

        LocalDateTime operateTime = LocalDateTime.now();

        String className = joinPoint.getTarget().getClass().getName();

        String methodName = joinPoint.getSignature().getName();

        Object[] methodParams = joinPoint.getArgs();

        long begin = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long costTime = end - begin;

        AdUserOperateLog userLog = new AdUserOperateLog();
        userLog.setId(null);
        userLog.setOperateEmpId(id);
        userLog.setOperateTime(operateTime);
        userLog.setClassName(className);
        userLog.setMethodName(methodName);
        userLog.setMethodParams(methodParams.toString());
        userLog.setCostTime(costTime);
        userLog.setReturnValue(result.toString());

        apUserOperateLogMapper.insert(userLog);

        return result;


    }
}
